package com.platform.web.controller.data;

import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.framework.util.ShiroUtils;
import com.platform.data.domain.RuleAlarm;
import com.platform.data.service.IRuleAlarmService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备告警规则Controller
 * 
 * @author wangqi
 * @date 2020-08-17
 */
@Controller
@RequestMapping("/data/alarm/rule")
public class RuleAlarmController extends BaseController
{
    private String prefix = "data/alarm/rule";

    @Autowired
    private IRuleAlarmService ruleAlarmService;

    @RequiresPermissions("data:alarm:rule:view")
    @GetMapping()
    public String alarm()
    {
        return prefix + "/alarm";
    }

    /**
     * 查询设备告警规则列表
     */
    @RequiresPermissions("data:alarm:rule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RuleAlarm ruleAlarm)
    {
        startPage();
        List<RuleAlarm> list = ruleAlarmService.selectRuleAlarmList(ruleAlarm);
        return getDataTable(list);
    }

    /**
     * 导出设备告警规则列表
     */
    @RequiresPermissions("data:alarm:rule:export")
    @Log(title = "设备告警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RuleAlarm ruleAlarm)
    {
        List<RuleAlarm> list = ruleAlarmService.selectRuleAlarmList(ruleAlarm);
        ExcelUtil<RuleAlarm> util = new ExcelUtil<RuleAlarm>(RuleAlarm.class);
        return util.exportExcel(list, "alarm");
    }

    /**
     * 新增设备告警规则
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备告警规则
     */
    @RequiresPermissions("data:alarm:rule:add")
    @Log(title = "设备告警规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RuleAlarm ruleAlarm)
    {
        ruleAlarm.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(ruleAlarmService.insertRuleAlarm(ruleAlarm));
    }

    /**
     * 修改设备告警规则
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RuleAlarm ruleAlarm = ruleAlarmService.getById(id);
        mmap.put("ruleAlarm", ruleAlarm);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备告警规则
     */
    @RequiresPermissions("data:alarm:rule:edit")
    @Log(title = "设备告警规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RuleAlarm ruleAlarm)
    {
        ruleAlarm.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(ruleAlarmService.updateRuleAlarm(ruleAlarm));
    }

    /**
     * 删除设备告警规则
     */
    @RequiresPermissions("data:alarm:rule:remove")
    @Log(title = "设备告警规则", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ruleAlarmService.deleteRuleAlarmByIds(ids));
    }
}
